<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>成果下载 | {$system_name}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="icon" type="image/png" href="{_ADMIN_STATIC_}/i/favicon.png">
    <!-- <link rel="apple-touch-icon-precomposed" href="{_ADMIN_STATIC_}/i/app-icon72x72@2x.png"> -->
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/amazeui.min.css" />
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/admin.css">
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/app.css">
    <!-- <script src="{_ADMIN_STATIC_}/js/echarts.min.js"></script> -->

</head>

<body data-type="index">
    <!-- 头部导航栏 -->
    {include file='public/header'}
    <!-- 左侧侧边栏 -->
    {include file='public/side'}
    <!-- 中间内容开始 -->

    <!-- 标题 -->
    <div class="tpl-content-page-title">
        {$system_name} -- 成果下载
    </div>
    <!-- 面包屑导航 -->
    <ol class="am-breadcrumb">
        <li><a href="{$index_home}" class="am-icon-home">成果管理首页</a></li>
        <li>成果管理下载页面</li>
    </ol>
    <!-- 提示信息 -->
    <div class="tpl-content-scope">
        <div class="note note-info">
            <h3> 成果下载起始页
                <span class="close" data-close="note"></span>
            </h3>
            <p>该页面涵盖了成果的所有信息以及功能</p>
            <p><span class="label label-danger">提示:</span>
                当前页面为成果自定义下载页面
            </p>
        </div>
    </div>

    <!-- 内容 -->
    <div class="tpl-portlet-components">
        <div class="row">
            <h1>下载选项</h1>
        </div>

        <div class="row">
            <!-- 成果分类 -->
            <div class="am-form am-form-horizontal">
                <div class="am-form-group">
                    <label for="classify_id" class="am-u-sm-3 am-form-label">成果分类 / Classify</label>
                    <div class="am-u-sm-9">
                        <input id="classify_id" type="text" autocomplete="off" list="classify">
                        <datalist id="classify">
                            {volist name="classify_all_data" id="row"}
                            <option value="{$row.name}" data-id="{$row.id}"></option>
                            {/volist}
                        </datalist>
                    </div>
                </div>
                <!-- 用户 -->
                <div class="am-form-group">
                    <label for="user_id" class="am-u-sm-3 am-form-label">用户 / User</label>
                    <div class="am-u-sm-9">
                        <input id="user_id" type="text" list="user" autocomplete="off">
                        <datalist id="user">
                            {volist name="user_data" id="row"}
                            <option value="{$row.name}" data-id="{$row.id}"></option>
                            {/volist}
                        </datalist>
                        <small></small>
                    </div>
                </div>
                <!-- 下载方式 -->
                <div class="am-form-group">
                    <label for="download_id" class="am-u-sm-3 am-form-label">下载方式 / Download</label>
                    <div class="am-u-sm-9">
                        <input id="download_id" type="text" list="download" autocomplete="off">
                        <datalist id="download">
                            <option value="全部" data-id="1"></option>
                            <option value="已通过" data-id="2"></option>
                            <option value="自定义" data-id="3"></option>
                        </datalist>
                        <small></small>
                    </div>
                </div>
            </div>

        </div>

        <div class="row">
            <div class="am-form-group">
                <div class="am-u-sm-3 am-u-sm-push-3">
                    <button type="button" class="am-btn am-btn-primary" onclick="download()">点击下载</button>
                    <!-- <button type="button" class="am-btn am-btn-primary" onclick="custom_download()">自定义下载</button> -->
                </div>
            </div>
        </div>

    </div>


    <!-- 中间内容结束 -->
    <!-- 底部 -->

    <script src="{_ADMIN_STATIC_}/lib/jquery/jquery.js"></script>
    <script src="{_ADMIN_STATIC_}/js/amazeui.min.js"></script>
    <script src="{_ADMIN_STATIC_}/js/app.js"></script>
    <script src="{_ADMIN_STATIC_}/lib/layer/layer.js"></script>
    <script>

        // 成果分类
        var classify_id = -1;
        $("#classify_id").on("input", function () {
            var $options = $("#classify").children();
            for (var i = 0; i < $options.length; i++) {
                if ($options.eq(i).val().trim() == $("#classify_id").val().trim()) {
                    classify_id = $options.eq(i).attr("data-id");
                    break;
                } else {
                    classify_id = -1;
                }
            }
        });

        // 用户
        var user_id = -1;
        $("#user_id").on("input", function () {
            var $options = $("#user").children();
            for (var i = 0; i < $options.length; i++) {
                if ($options.eq(i).val().trim() == $("#user_id").val().trim()) {
                    user_id = $options.eq(i).attr("data-id");
                    break;
                } else {
                    user_id = -1;
                }
            }
        });

        // 下载方式
        var download_id = 1;
        $("#download_id").on("input", function () {
            var $options = $("#download").children();
            for (var i = 0; i < $options.length; i++) {
                if ($options.eq(i).val().trim() == $("#download_id").val().trim()) {
                    download_id = $options.eq(i).attr("data-id");
                    break;
                } else {
                    download_id = 1;
                }
            }
        });

        // 自定义下载
        function custom_download() {
            return redirect_url("{:url('AchievementManage/custom')}");
        }

        // 下载
        function download() {
            // 必须要有分类
            if (classify_id == -1) {
                return layer.msg('必须选择一个成果分类', { icon: 2, time: 900 });
            }

            // 判断下载方式
            // 下载全部
            if (download_id == 1) {
                var data = {
                    'cid': classify_id,
                    'uid': user_id,
                    'status': -1,
                };
            }
            // 下载已通过
            else if (download_id == 2) {
                var data = {
                    'cid': classify_id,
                    'uid': user_id,
                    'status': 0,
                };
            }
            else if (download_id == 3) {
                return redirect_url("{:url('AchievementManage/custom')}?cid=" + classify_id + "&uid=" + user_id);
            }
            else {
                return layer.msg('下载方式错误', { icon: 2, time: 900 });
            }

            var url = "{:url('AchievementManage/download_achievement')}";
            $.ajax({
                url: url,
                data: data,
                type: "POST",
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        redirect_url(res.url);
                        return layer.msg(res.msg, { icon: 1, time: 900 });
                    } else {
                        return layer.msg(res.msg, { icon: 2, time: 900 });
                    }

                },
            });
        }

        // 重定向
        function redirect_url(url) {
            window.location.href = url;
        }

    </script>
</body>

</html>